package tiepbm.app.ui.slidemenu;

import java.util.List;

import tiepbm.app.ui.slidemenu.SlideMenuAnimationContainer.MenuItemSelectedAction;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.tiepbm.cucphamgiadinh.R;

public class SlideMenu implements SlideMenuAnimationContainer.Listener {

	// public final static String[] slideMenuOptions = { "Activity 1",
	// "Activity 2", "Activity 3" };
	public final static String TAG = "SlideMenu";
	// protected static final String TextView = null;

	private Context context;
	private SlideMenuAnimationContainer slideMenuAnimationContainer;

	public SlideMenu(Context context,
			SlideMenuAnimationContainer mainAnimationLayout) {

		this.context = context;
		this.slideMenuAnimationContainer = mainAnimationLayout;
	}

	public void init(List<SlideMenuItem> slideMenuList) {

		final Activity activity = (Activity) context;

		// Set Content's show menu button's action
		ImageButton showMenuButton = (ImageButton) activity
				.findViewById(R.id.content_button);
		showMenuButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				slideMenuAnimationContainer.toggleSlideMenu();
			}
		});

		// Contruct the SlideMenu items here, you can replace this ArrayAdapter
		// with your customr ArrayAdapter
		// List<SlideMenuItem> slideMenuList = new ArrayList<SlideMenuItem>();
		// slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Header,
		// "Header 1"));
		// slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Activity,
		// "Activity 1"));
		// slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Header,
		// "Header 2"));
		// slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Activity,
		// "Activity 2"));
		// slideMenuList.add( new SlideMenuItem( SlideMenuItemType.Activity,
		// "Activity 3"));

		SlideMenuArrayAdapter slideMenuArrayAdaptar = new SlideMenuArrayAdapter(
				context, android.R.layout.simple_list_item_1, slideMenuList);

		ListView menuListView = (ListView) activity
				.findViewById(R.id.slideMenuListView);
		menuListView.setAdapter(slideMenuArrayAdaptar);
		menuListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// Close sidebar
				slideMenuAnimationContainer.closeSlideMenuAndActOnClick(parent,
						view, position, id);
			}
		});

		// Implement your logic here within the execute() function when an item
		// is clicked with in the SlideMenu
		// Called after the SlideMenu collapses.
		slideMenuAnimationContainer
				.setMenuItemSelectedAction(new MenuItemSelectedAction() {

					@Override
					public void execute(AdapterView<?> parent, View view,
							int position, long id) {

						// Only act when a ListViewItem with type Activity is
						// clicked
						// TextView textView = (TextView)view.findViewById(
						// R.id.MenuActivityListViewItem_text);
						// if( textView != null && textView.getId() ==
						// R.id.MenuActivityListViewItem_text){
						//
						// //Start new activity
						// CharSequence selectedActivityName =
						// textView.getText();
						//
						// Intent intent;
						// if( selectedActivityName.equals( "Activity 1")) {
						// intent = new Intent( activity, Live.class);
						// }
						// else if( selectedActivityName.equals( "Activity 2"))
						// {
						// intent = new Intent( activity, Activity2.class);
						// }
						// else {
						// intent = new Intent( activity, Activity3.class);
						// }
						// intent.setFlags( Intent.FLAG_ACTIVITY_NO_ANIMATION);
						// activity.startActivity( intent);
						// }
					}
				});
	}

	// Callback of SlideMenuAnimationContainer.Listener to monitor status of
	// SlideMenu
	@Override
	public void onSlideMenuOpened() {
		Log.d(TAG, "opened");
	}

	// Callback of SlideMenuAnimationContainer.Listener to monitor status of
	// SlideMenu
	@Override
	public void onSlideMenuClosed() {
		Log.d(TAG, "closed");
	}

	// Callback of SlideMenuAnimationContainer.Listener to monitor status of
	// SlideMenu
	@Override
	public boolean onContentTouchedWhenOpening() {
		// The content area is touched when sidebar opening, close sidebar
		Log.d(TAG, "going to close sidebar");
		slideMenuAnimationContainer.closeSlideMenu();
		return true;
	}
}
